// app.js
import { parseQueryString } from './utils/index'
import { setTokenValue } from './utils/storage'
import { channelConfig, getSceneBySceneId, getActivityConfig } from './api/index'
App({
  onLaunch() {
  },
  globalData: {
    info: '',
    gdtVid: '',
    showSwitch: '1',   // show是否触发 0-关 1-开
    extraMsg: {
      channelCode: '',  // 渠道码
      sourceTag: '',  // 来源标记
      inviterCode: '' // 邀请码
    },
    packageObj: {
      siqSource: '',
      packageCode: '',
      packageName: '',
      discountAmt: '',
    },
    token: '',
    activityConfig: {
      activitySwitch: "0",
      sharePath: "/pages/guide/guide",
      qrPath: "pages/guide/guide",
      shareTitle: "你手边最懂金税四期的AI专家，随时随地掌上咨询",
    }
  },
  async onShow(options) {
    console.info('onShow')
    if (this.globalData.showSwitch == '0') {
      this.globalData.showSwitch = '1'
      return
    }
    console.info(options)
    console.info(options.query)
    let query = options.query
    if (query.scene) {
      // inviterValue*channelCodeValue
      const scene = decodeURIComponent(query.scene)
      let result = await getSceneBySceneId({ sceneId: scene })
      let data = result && result.scene
      let obj = {}
      if (data) {
        obj = parseQueryString(data)
      } else {
        obj = parseQueryString(scene)
      }
      query = Object.assign(options.query, obj)
      this.setInitData(query)
    } else {
      this.setInitData(query)
    }
    // 活动配置
    let activeRsult = await getActivityConfig()
    if (activeRsult) {
      this.globalData.activityConfig = activeRsult
    }
  },
  async setInitData(query) {
    let queryData = query || {}
    // 邀请码
    this.globalData.extraMsg.inviterCode = queryData.inviter || ''
    // 来源标记
    this.globalData.extraMsg.sourceTag = queryData.sourceTag || ''
    // 套餐包
    this.globalData.packageObj.packageCode = queryData.packageCode || ''
    this.globalData.packageObj.packageName = queryData.packageName || ''
    this.globalData.packageObj.discountAmt = queryData.discountAmt || ''
    this.globalData.packageObj.siqSource = queryData.siqSource || ''
    // token
    if (queryData.token) {
      this.globalData.token = queryData.token || ''
    }
    // 渠道码
    if (queryData.channelCode) {
      let result = await channelConfig({
        channelCode: queryData.channelCode
      })
      if (result) {
        this.globalData.extraMsg.channelCode = queryData.channelCode
      } else {
        this.globalData.extraMsg.channelCode = ''
      }
    } else {
      this.globalData.extraMsg.channelCode = ''
    }
    console.info('extraMsg')
    console.info(this.globalData.extraMsg)
    // 广告进入
    this.globalData.gdtVid = queryData.gdtVid || ''
  }
})
